package cn.nawang.ebeim.api.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.api.bean.DatasourceBean;
import cn.nawang.ebeim.api.bean.DatasourceMenuBean;
import cn.nawang.ebeim.api.bean.IsOnLineBean;
import cn.nawang.ebeim.api.comm.BaseController;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.BackStrategy;
import cn.nawang.ebeim.api.domain.DataSourceWrapper;
import cn.nawang.ebeim.api.domain.Datasource;
import cn.nawang.ebeim.api.domain.DatasourceConfig;
import cn.nawang.ebeim.api.domain.User;
import cn.nawang.ebeim.api.service.DatasourceService;
import cn.nawang.ebeim.api.service.HeartLogService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.JsonUtil;
import cn.nawang.ebeim.api.utils.ValidateUtil;
import cn.nawang.ebeim.api.vo.DatasourceView;

/**
 * @ClassName: DatasourceController
 * @Description: 终端控制器
 * @author 张建峰
 * @date 2015年8月10日09:41:41
 * @version v1.0
 */
@RestController
public class DatasourceController extends BaseController {
  private static final Logger LOG = LoggerFactory.getLogger(DatasourceController.class);

  @Autowired
  private DatasourceService datasourceService;

  @Autowired
  private HeartLogService heartLogService;

  @RequestMapping(value = "/datasource/list/search", method = RequestMethod.GET)
  public Object findByDatasource(Datasource datasource, Pages page, HttpServletRequest request) {
    LOG.debug("/datasource/list/search {}", datasource, page);
    Map<String, Object> map = new HashMap<String, Object>();
    PageList pagelist = datasourceService.findByDatasource(datasource, page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findByDatasource {}", map);
    return map;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 运行监控模块：查看数据源集合
   * @param @param request
   * @param @param userId
   * @param @param mac
   * @param @param isMac
   * @param @return
   * @return Object
   * @date 2015年11月10日下午5:12:04
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/monitor/datasource_list", method = RequestMethod.GET)
  public Object findDatasourceListByUserId(HttpServletRequest request, String userId, String mac,
      String isMac) {
    LOG.debug("/token/datasource/list {}", userId, mac);
    Map<String, Object> map = new HashMap<String, Object>();
    List datasources = new ArrayList();
    if (isMac.equals("0")) {
      datasources = datasourceService.findListByUserId(null, userId);
    } else {
      datasources = datasourceService.findListByUserId(mac, userId);
    }
    map.put("list", datasources);
    map.put("code", Constant.CODE_SUCCESS);
    map.put("safeBox", datasourceService.findSafeBoxByUserId(null, userId));
    LOG.debug("findDatasourceListByUserId {}", map);
    return map;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取数据源执行计划
   * @param @param request
   * @param @param id
   * @param @param mac
   * @param @return
   * @return Object
   * @date 2015年11月10日下午8:15:12
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/monitor/plan", method = RequestMethod.GET)
  public Object findDatasourcePlanByUserId(HttpServletRequest request, String id, String mac,
      String nativeSel, String useSel, String onlineSel) {
    LOG.debug("/token/datasource/list {}", id, mac);
    Map<String, Object> map = new HashMap<String, Object>();
    List<IsOnLineBean> list = heartLogService.getIsOnLineBeansList(id, onlineSel);
    int otherCountSum = 0;
    int otherActiveCountSum = 0;

    for (IsOnLineBean obj : list) {
      Map<String, Object> objMap = new HashMap<String, Object>();
      int sum = datasourceService.countByUserId(id, obj.getMac(), null, "0");// 总数据源数目
      int active = datasourceService.countByUserId(id, obj.getMac().toString(), "0", "0");// 启用数据源数目
      List datasources = datasourceService.findPlanListByUserId(obj.getMac(), id, useSel);
      if (useSel.equals("1")) {
        obj.setDsSum(active);
      } else {
        obj.setDsSum(sum);
      }
      obj.setDsOnSum(active);
      objMap.put("info", JsonUtil.toString(obj));
      objMap.put("list", datasources);
      if (nativeSel.equals("1")) {
        otherCountSum = 0;
        otherActiveCountSum = 0;
        if (obj.getMac().equals(mac)) {
          map.put(obj.getMac(), objMap);
        }
      } else {
        if (!obj.getMac().equals(mac)) {
          otherCountSum += sum;
          otherActiveCountSum += active;
        }
        map.put(obj.getMac(), objMap);
      }
    }
    // 计算其他数据源的数据源总和
    Map<String, Object> otherMap = new HashMap<String, Object>();
    otherMap.put("activeCount", otherActiveCountSum);
    if (useSel.equals("1")) {
      otherMap.put("otherCount", otherActiveCountSum);
    } else {
      otherMap.put("otherCount", otherCountSum);
    }
    otherMap.put("mac", mac);
    map.put("other", otherMap);
    LOG.debug("findDatasourceListByUserId {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 提供给api显示的数据源列表
   * @param page
   * @param request
   * @return Object
   * @date Oct 16, 20159:10:37 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/datasource/page_list", method = RequestMethod.GET)
  public Object findPageList(Pages page, HttpServletRequest request) {
    LOG.info("/datasource/page_list {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();

    User user = this.getUserLoginInfo(request);
    if (Constant.ROLE_USER.equals(user.getRole())) {
      // 客户编号
      String createUserId = user.getId();
      if (!StringUtils.isEmpty(createUserId)) {
        dtovect.add(new SearchFormParams("d.createUserId", createUserId, "createUserId",
            SearchFormParams.STRING_TYPE));
      }
    } else {
      // 客户编号
      String customerId = user.getCustomerId();
      if (!StringUtils.isEmpty(customerId)) {
        dtovect.add(new SearchFormParams("d.customerId", customerId, "customerId",
            SearchFormParams.STRING_TYPE));
      }
    }
    // 数据源名称
    String name = request.getParameter("name");
    if (!StringUtils.isEmpty(name)) {
      dtovect.add(new SearchFormParams("d.name", name, "name", SearchFormParams.STRING_TYPE));
    }
    // 运行状态
    String status = request.getParameter("status");
    if (!StringUtils.isEmpty(status)) {
      dtovect.add(new SearchFormParams("d.status", Integer.valueOf(status), "status",
          SearchFormParams.INTEGER_TYPE));
    }
    // 数据源状态
    String dsStatus = request.getParameter("dsStatus");
    if (!StringUtils.isEmpty(dsStatus)) {
      dtovect.add(new SearchFormParams("d.dsStatus", Integer.valueOf(dsStatus), "dsStatus",
          SearchFormParams.INTEGER_TYPE));
    }
    // 备份类型
    String type = request.getParameter("backupType");
    if (!StringUtils.isEmpty(type)) {
      dtovect.add(new SearchFormParams("b.type", type, "type", SearchFormParams.STRING_TYPE));
    }
    // 数据源类型
    String dsType = request.getParameter("dsType");
    if (!StringUtils.isEmpty(dsType)) {
      dtovect.add(new SearchFormParams("d.dsType", dsType, "dsType", SearchFormParams.STRING_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = datasourceService.findAllByPaging(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findPageList {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: api获取数据源对象使用接口
   * @param id
   * @return Object
   * @date Oct 20, 201511:02:04 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/datasource/get", method = RequestMethod.GET)
  public Object findOne(String id) {
    LOG.debug("/datasource/get {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    DatasourceBean bean = datasourceService.findDatasourceBean(id);
    if (bean == null) {
      map.put(Constant.JSON_CODE, Constant.CODE_404);
    } else {
      map.put(Constant.JSON_CODE, Constant.CODE_200);
      map.put("obj", bean);
    }
    LOG.debug("/datasource/get {}", map);
    return map;
  }

  /**
   * @author 张建峰
   * @Description: 根据id查询对象
   * @param @param id
   * @param @return
   * @return DatasourceBean
   * @date 2015年8月7日上午8:39:37
   *
   * @修改人 张建峰
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/get", method = RequestMethod.GET)
  public Object findById(String id) {
    LOG.debug("/token/datasource/get {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    DatasourceBean bean = datasourceService.findDatasourceBean(id);
    if (bean == null) {
      map.put(Constant.JSON_CODE, Constant.CODE_404);
    } else {
      map.put(Constant.JSON_CODE, Constant.CODE_200);
      map.put("obj", bean);
    }
    LOG.debug("findById {}", map);
    return map;
  }


  /**
   * 
   * @author fanyb
   * @Description: 根据终端编号查找数据源列表
   * @param terminalId 终端编号
   * @return Map 返回根据终端编号查找数据源列表
   * @date Aug 17, 201510:05:33 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/find/terminalId", method = RequestMethod.GET)
  public Map<String, Object> findByTerminalId(String terminalId) {
    LOG.debug("/token/datasource/find/terminalId {}", terminalId);
    String property = "terminalId";
    List<DatasourceView> datasources =
        datasourceService.findDatasourceViewByParam(property, terminalId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("list", datasources);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByTerminalId {}", map);
    return map;
  }

  /**
   * @author zhangjf
   * @Description: 根据终端id分页查询（ui端用到）
   * @param @param terminalId
   * @param @param currPage
   * @param @param pageSize
   * @param @return
   * @return Map<String,Object>
   * @date 2015年10月12日下午3:19:40
   *
   * @修改人 zhangjf
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/find/terminalId/pages", method = RequestMethod.GET)
  public Map<String, Object> findByTerminalIdByPage(String terminalId, Integer currPage,
      Integer pageSize) {
    LOG.debug("/token/datasource/find/terminalId/pages {}", terminalId, currPage, pageSize);
    String property = "terminalId";
    JSONObject json =
        datasourceService.findDatasourceViewByPage(property, terminalId, currPage, pageSize);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("json", json);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByTerminalIdByPage {}", map);
    return map;
  }

  /**
   * @author zhangjf
   * @Description: 根据客户id分页查询（ui端用到）
   * @param @param customerId
   * @param @param currPage
   * @param @param pageSize
   * @param @return
   * @return Map<String,Object>
   * @throws UnsupportedEncodingException
   * @date 2015年10月12日下午3:19:40
   *
   * @修改人 zhangjf
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/find/createuserid/pages", method = RequestMethod.GET)
  public Map<String, Object> findByCreateUserIdByPage(Pages page, HttpServletRequest request)
      throws UnsupportedEncodingException {
    LOG.info("/token/datasource/find/createuserid/pages {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 数据源名称
    String name = request.getParameter("name");
    name = java.net.URLDecoder.decode(name, "utf-8");
   /* name = new String(name.getBytes("ISO8859-1"), "UTF-8");*/
    if (!StringUtils.isEmpty(name)) {
      dtovect.add(new SearchFormParams("d.name", name, "name", SearchFormParams.STRING_TYPE));
    }
    // 数据源状态
    String status = request.getParameter("status");
    if (!StringUtils.isEmpty(status)) {
      dtovect
          .add(new SearchFormParams("d.status", status, "status", SearchFormParams.INTEGER_TYPE));
    }
    // 数据源状态
    String dsStatus = request.getParameter("dsStatus");
    if (!StringUtils.isEmpty(dsStatus)) {
      dtovect.add(new SearchFormParams("d.dsStatus", Integer.valueOf(dsStatus), "dsStatus",
          SearchFormParams.INTEGER_TYPE));
    }
    // 用户id
    String createUserId = request.getParameter("createUserId");
    if (!StringUtils.isEmpty(createUserId)) {
      dtovect.add(new SearchFormParams("d.createUserId", createUserId, "createUserId",
          SearchFormParams.INTEGER_TYPE));
    }
    // 数据源类型
    String dsType = request.getParameter("dsType");
    if (!StringUtils.isEmpty(dsType)) {
      dtovect
          .add(new SearchFormParams("d.dsType", dsType, "dsType", SearchFormParams.INTEGER_TYPE));
    }
    // 终端类型
    String terminalId = request.getParameter("terminalId");
    if (!StringUtils.isEmpty(terminalId)) {
      dtovect.add(new SearchFormParams("d.terminalId", terminalId, "terminalId",
          SearchFormParams.INTEGER_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = datasourceService.findAllByPaging(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findByCreateUserIdByPage {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新数据源
   * @param datasource 数据源对象
   * @return Map<String,Object>
   * @date Aug 27, 201510:23:03 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/update", method = RequestMethod.POST)
  public Map<String, Object> updataDatasource(Datasource datasource,
      DatasourceConfig datasourceConfig, BackStrategy bs, HttpServletRequest request) {
    LOG.debug("/token/datasource/update {}", datasource, datasourceConfig, bs);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    User user = this.getUserLoginInfo(request);
    boolean isSuccess = datasourceService.updateDatasource(datasource, datasourceConfig, bs, user);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
    }
    LOG.debug("updataDatasource {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取数据源对象（非后台用到）
   * @param id 数据源编号
   * @return Map<String,Object>
   * @date Aug 27, 20152:23:36 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/get/id", method = RequestMethod.GET)
  public Map<String, Object> findDatasource(String id) {
    LOG.debug("/token/datasource/get/id {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    DatasourceView datasource = datasourceService.findDatasourceView(id);
    map.put("obj", datasource);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findDatasource {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增数据源
   * @param datasource
   * @param datasourceConfig
   * @param bs
   * @return Map<String,Object>
   * @date Sep 17, 201511:24:34 AM
   *
   * @修改人 张建峰
   * @修改日期 2015年9月18日10:54:41
   */
  @RequestMapping(value = "/datasource/add", method = RequestMethod.POST)
  public Map<String, Object> add(Datasource datasource, DatasourceConfig datasourceConfig,
      BackStrategy bs, HttpServletRequest request) {
    LOG.debug("/datasource/add {}", datasource, datasourceConfig, bs);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    User user = this.getUserLoginInfo(request);
    JSONObject json = datasourceService.addDatasource(datasource, datasourceConfig, bs, user);
    if (json.getString("code").equals(Constant.CODE_SUCCESS)) {
      map.put("code", Constant.CODE_SUCCESS);
    }
    LOG.debug("add {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 提供给客户端的新增数据源接口
   * @param datasource
   * @param datasourceConfig
   * @param bs
   * @return Map<String,Object>
   * @date Sep 23, 20159:08:17 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/web_client/datasource/add", method = RequestMethod.POST)
  public Map<String, Object> addDatasourceClient(@RequestBody DataSourceWrapper dataSourceWrapper,
      HttpServletRequest request) {
    LOG.info("Web client addDatasourceClient {}" + dataSourceWrapper);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    User user = dataSourceWrapper.getUser();
    Datasource datasource = dataSourceWrapper.getDatasource();
    DatasourceConfig datasourceConfig = dataSourceWrapper.getDatasourceConfig();
    BackStrategy bs = dataSourceWrapper.getBackStrategy();
    if (!validAddDatasource(map, datasource, datasourceConfig, user)) {
      return map;
    }
    JSONObject json = datasourceService.addDatasource(datasource, datasourceConfig, bs, user);
    if (json.getIntValue("code") == Constant.CODE_SUCCESS) {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("dsId", json.getString("dsId"));
    } else {
      map.put("code", Constant.CODE_SUCCESS);
    }
    LOG.debug("addDatasourceClient {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新（ui用到的）
   * @param dataSourceWrapper
   * @param request
   * @return Map<String,Object>
   * @date Oct 20, 201511:08:32 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/web_client/datasource/update", method = RequestMethod.POST)
  public Map<String, Object> updataDatasourceClient(
      @RequestBody DataSourceWrapper dataSourceWrapper, HttpServletRequest request) {
    LOG.info("Web client updataDatasourceClient {}" + dataSourceWrapper);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    User user = dataSourceWrapper.getUser();
    Datasource datasource = dataSourceWrapper.getDatasource();
    DatasourceConfig datasourceConfig = dataSourceWrapper.getDatasourceConfig();
    BackStrategy bs = dataSourceWrapper.getBackStrategy();
    boolean isSuccess = datasourceService.updateDatasource(datasource, datasourceConfig, bs, user);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
    }
    LOG.debug("updataDatasourceClient {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增数据源验证
   * @param map
   * @param datasource
   * @param datasourceConfig
   * @return boolean
   * @date Oct 30, 20154:37:51 PM
   *
   * @修改人
   * @修改日期
   */
  public boolean validAddDatasource(Map<String, Object> map, Datasource datasource,
      DatasourceConfig datasourceConfig, User user) {

    boolean isFile = Constant.DATESOURCE_TYPE_ZERO.equals(datasource.getDsType());
    boolean isSafeBox = Constant.DATESOURCE_TYPE_TWO.equals(datasource.getDsType());

    // 如果是文件夹类型或保险箱类型，则根路径不能为空
    if ((isFile || isSafeBox) && ValidateUtil.isEmpty(datasourceConfig.getRoot())) {
      map.put("validCode", Constant.CODE_ERROR);
      map.put("message", "数据源根目录不能为空");
      return false;
    }

    // 如果是文件夹类型，同设备，同用户，不能存在同根目录
    if (Constant.DATESOURCE_TYPE_ZERO.equals(datasource.getDsType())) {
      // terminalid同一个设备下的数据源root是不能相同的
      Datasource ds = datasourceService.findByDeviceAndRoot(datasource.getTerminalId(),
          datasourceConfig.getRoot(), user.getId());
      if (!ValidateUtil.isNull(ds)) {
        map.put("validCode", Constant.CODE_ERROR);
        map.put("message", "本设备已存在相同的数据源");
        return false;
      }
    }

    return true;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找当前用户的设备名称
   * @param createUserId
   * @return Map<String,Object>
   * @date Nov 11, 20152:13:47 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/web_client/datasource/device_name/terminal_id", method = RequestMethod.GET)
  public Map<String, Object> findBindDeviceNameByTerminalId(String createUserId) {
    LOG.info("Web client updataDatasourceClient {}" + createUserId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    List<DatasourceMenuBean> list = datasourceService.findBindDeviceNameByTerminalId(createUserId);
    map.put("list", list);
    map.put("code", Constant.CODE_SUCCESS);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量更新数据源状态
   * @param ids
   * @param status
   * @return Map<String,Object>
   * @date Nov 12, 20154:32:46 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/web_client/update/datasources/status", method = RequestMethod.GET)
  public Map<String, Object> updateDatasourcesStatus(String[] ids, int dsStatus) {
    LOG.info("/token/web_client/update/datasources/status {}" + ids, dsStatus);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    boolean isSuccess = datasourceService.updateDatasourcesStatus(ids, dsStatus);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
    }
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找是否有在备份的数据源
   * @param ids
   * @return Map<String,Object>
   * @date Nov 27, 20152:28:34 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/web_client/find/datasources/status", method = RequestMethod.GET)
  public Map<String, Object> findDatasourcesResult(String[] ids) {
    LOG.info("/token/web_client/find/datasources/status {}" + ids);
    Map<String, Object> map = new HashMap<String, Object>();
    /* StringBuffer sb = new StringBuffer(); */
    List<Datasource> list =
        datasourceService.findDatasources(ids, Constant.DATESOURCE_RUN_STATUS_TYPE_ONE);
    if (!list.isEmpty() && list != null) {
      /*
       * for (int i = 0; i < list.size(); i++) { sb.append("数据源：" + list.get(i).getName() +
       * "，正在备份中，请备份完成后再进行操作。</br>"); }
       */
      map.put("code", Constant.CODE_ERROR);
      map.put("desc", "选择列表中有数据源正在备份中，请备份完成后再进行操作。");
      return map;
    }
    map.put("code", Constant.CODE_SUCCESS);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据创建人id和状态查找数据源列表
   * @param createUserId
   * @param dsType
   * @param request
   * @return Map<String,Object>
   * @date Nov 27, 20152:34:08 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/datasource/find/create_user_id/ds_type", method = RequestMethod.GET)
  public Map<String, Object> findByCreateUserIdAndStatus(String createUserId, String dsType,
      HttpServletRequest request) {
    LOG.info("/token/datasource/find/create_user_id/ds_type {}", createUserId, dsType);
    Map<String, Object> map = new HashMap<String, Object>();
    List<Datasource> list = datasourceService.findByCreateUserIdAndDsType(createUserId, dsType);
    map.put("list", list);
    LOG.debug("findByCreateUserIdAndStatus {}", map);
    return map;
  }
}
